(function (app) {
	var app = angular.module('myApp', []);
    app.controller("CrsCtrl", function ($scope,$http,$timeout) {
    	
    	//返回上一步
        $("#goBack").click(function(){
    		window.location.href="/pad_marketing/main/gotoHealthContent.action";
    	});
        
        //重新改写弹出框
    	$scope.alertHomebtn=function(){
    		$("body").css("overflow","hidden");
    		$("#gz-aleratAll").css("height",$(window).height()); 
    		$("#gz-aleratAll").css("top",document.body.scrollTop); 
    		$("#gz-aleratAll").removeClass("hide");
    		$(".circle-panel").removeClass("hide");
    	};
    	$scope.alertHomebtnClose=function(){
    		$("body").css("overflow","visible");
    		$("#gz-aleratAll").addClass("hide");
    		$(".circle-panel").addClass("hide");
    	};
        
        $timeout(function(){//延迟加载数据
    		var params = JSON.parse(sessionStorage.appBase); 
			//初始化页面调取数据
    		padMarketing.ajaxNormal('/pad_marketing/crsAction/editCRS.action',params,
				function(data){
					$scope.app = data.app;
					$scope.countries = data.countries;
					console.log($scope.app);
					
					 // 找到第一个需要填写问卷的人
		            for (var i = 0; i < $scope.app.cps.length; i++) //循环数据
		            {
		                if ($scope.app.cps[i].needCRS)
		                {
		                	$scope.$apply(function(){
			                	//数据模型被改变,页面不刷新的解决办法$scope.$apply
			                    $scope.cp = $scope.app.cps[i];
			                    $scope.countries = $scope.countries;
		                	});
		                    break;
		                }
		            }
		            

					$scope.$apply(function(){
	                	//数据模型被改变,页面不刷新的解决办法$scope.$apply
	                    $scope.countries = $scope.countries;
                	});
					if(myScroll) {
						myScroll.refresh();
					}
				},
				function(data){
					 $scope.app = null;
				}
			);
		},300);
    	
        //投被保人点击事件
        $scope.SwitchIns = function (cp) {
            if ($scope.cp === cp) return;
            if ($scope.myForm.$invalid){
            	alertMsg("请先完成当前问卷。");
                return;
            }
            $scope.cp = cp;
        };
    
       
        
        $scope.addTaxArea = function (crsV1) {//添加更多地区
			
            var maxNo = 0;
            angular.forEach(crsV1.taxCountries, function (tc) {
                if (tc.subNo > maxNo) maxNo = tc.subNo;
            });
            crsV1.taxCountries.push({ subNo: maxNo + 1 });
            
            $timeout(function(){//添加div之后计算高度
	            if(myScroll) {
					myScroll.refresh();
				}
            },100);
        };
        
        //删除更多地区
        if (!Array.prototype.remove) {
            Array.prototype.remove = function (elt /*, from*/) {
                var index = this.indexOf(elt);
                if (index > -1) {
                    this.splice(index, 1);
                }
            };
        }
        $scope.removeTaxArea = function (tc) {
            if ($scope.cp.crsV1.taxCountries.length == 1) return;
            $scope.cp.crsV1.taxCountries.remove(tc);
        };
        
        
        
        
        
        
        
      //下一步提交数据
    	$scope.gzNext = function () {
    		
     		var param = $scope.app;
     		
     		padMarketing.ajaxNormal('/pad_marketing/crsAction/saveCRS.action',param,
 				function(data){
	     			
     			
     				if($scope.app.appBase.productGroup3 == "UNIT LINK"){
     					//如果保单为投连险时 需跳转到风险评估页面
     	    			window.location = "/pad_marketing/main/gotoRiskAssess.action";
     				}
     				else{
     					sessionStorage.setItem('promptUrl','gotoHealthContent.action')
     					//如果保单不为投连险时直接跳转到投保提示书页面
     					window.location = "/pad_marketing/main/gotoCoverPrompt.action";
     				}

     				
 				},
 				function(data){
 					//错误提示
 				}
 			);
    		
        }
        
        
    });
    
    //清除已填选项
    app.controller("TaxCountryCtrl", function ($scope) {
        $scope.$watch('tc.provideTaxPayeeId', function (newVal) {
            if (newVal == 'N') $scope.tc.taxPayeeId = null;
        });
        $scope.$watch('tc.notProvideTaxPayeeIdReason', function (newVal) {
            if (newVal == 'A') $scope.tc.noTaxPayeeIdReason = null;
        });
    });
    
}());